// SimulCtrl.cpp : implementation file
//

#include "stdafx.h"
#include "voldia.h"
#include "SimulCtrl.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern CVolDiaApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CSimulCtrl dialog


CSimulCtrl::CSimulCtrl(CWnd* pParent /*=NULL*/)
	: CDialog(CSimulCtrl::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSimulCtrl)
	m_SysStatus = _T("");
	m_StatusA = _T("");
	m_StatusB = _T("");
	m_txtStation = _T("");
	//}}AFX_DATA_INIT
}


void CSimulCtrl::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSimulCtrl)
	DDX_Control(pDX, IDC_B_STATUS, m_StatusBCtrl);
	DDX_Control(pDX, IDC_A_STATUS, m_StatusACtrl);
	DDX_Control(pDX, IDC_BYPASS_B, m_BypassBCtrl);
	DDX_Control(pDX, IDC_BYPASS_A, m_BypassACtrl);
	DDX_Control(pDX, IDC_RESET, m_Reset);
	DDX_Text(pDX, IDC_SYS_STATUS, m_SysStatus);
	DDX_Text(pDX, IDC_A_STATUS, m_StatusA);
	DDX_Text(pDX, IDC_B_STATUS, m_StatusB);
	DDX_Text(pDX, IDC_STATION, m_txtStation);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSimulCtrl, CDialog)
	//{{AFX_MSG_MAP(CSimulCtrl)
	ON_BN_CLICKED(IDC_RESET, OnReset)
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	ON_EN_SETFOCUS(IDC_SYS_STATUS, OnSetfocusSysStatus)
	ON_EN_SETFOCUS(IDC_A_STATUS, OnSetfocusAStatus)
	ON_EN_SETFOCUS(IDC_B_STATUS, OnSetfocusBStatus)
	ON_BN_CLICKED(IDC_BYPASS_A, OnBypassA)
	ON_BN_CLICKED(IDC_BYPASS_B, OnBypassB)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSimulCtrl message handlers

BOOL CSimulCtrl::OnInitDialog(){
	CDialog::OnInitDialog();
	RETSTAT		retSt;
	RETCOND		retCnd;
	int			simFlag;
	int			ctrlFlag;
	TAGIDX		tagAIdx;
	TAGIDX		tagBIdx;
	TAGVALEX	tagAVal;
	TAGVALEX	tagBVal;
	TCHAR		msg[128];

	this->m_loggedUser = 0;

	this->SetControls();

	if(theApp.m_pDlg->m_Station == STATION_1)
		this->m_txtStation.Format(_T("operator interface 1"));
	else if(theApp.m_pDlg->m_Station == STATION_2)
		this->m_txtStation.Format(_T("operator interface 2"));
	else
		this->m_txtStation.Format(_T("unknown"));

	retCnd = GetTagIdx(TWN_BYPASS_A, &tagAIdx);
	if(retCnd != R_NORMAL){
		_stprintf_s(msg, _T("Error retrieving tag \"%s\" idx."), TWN_BYPASS_A);
		MessageBox(msg,  _T("Error"), MB_OK | MB_ICONERROR);
		return FALSE;
	}
	retCnd = GetTagIdx(TWN_BYPASS_B, &tagBIdx);
	if(retCnd != R_NORMAL){
		_stprintf_s(msg, _T("Error retrieving tag \"%s\" idx."), TWN_BYPASS_B);
		MessageBox(msg,  _T("Error"), MB_OK | MB_ICONERROR);
		return FALSE;
	} 

	retSt = sysInSimulation(&simFlag);
	if(retSt != RT_NORMAL){
		MessageBox(_T("Error checking system simulation status."),  _T("Error"), MB_OK | MB_ICONERROR);
		return(FALSE);
	}

	retSt = sysInControl(&ctrlFlag);
	if(retSt != RT_NORMAL){
		MessageBox(_T("Error checking system control status."),  _T("Error"), MB_OK | MB_ICONERROR);
		return(FALSE);
	}

	if(simFlag == ctrlFlag){
		MessageBox(_T("Undefined system simulation/control mode."),  _T("Error"), MB_OK | MB_ICONERROR);
		return(FALSE);
	}
	else if((simFlag == 0) && (ctrlFlag == 1)){
		this->m_SysStatus.Format(_T("system in control"));
		ClearStationTCFlag();
	}
	else{
		this->m_SysStatus.Format(_T("system in simulation"));
		ClearStationTCFlag();
	}

	retCnd = TagRead(&tagAIdx, &tagAVal);
	if(retCnd != R_NORMAL){
		_stprintf_s(msg, _T("Error reading tag \"%s\" value."), TWN_BYPASS_A);
		MessageBox(msg,  _T("Error"), MB_OK | MB_ICONERROR);
		return FALSE;
	}
	retCnd = TagRead(&tagBIdx, &tagBVal);
	if(retCnd != R_NORMAL){
		_stprintf_s(msg, _T("Error reading tag \"%s\" value."), TWN_BYPASS_B);
		MessageBox(msg,  _T("Error"), MB_OK | MB_ICONERROR);
		return FALSE;
	}

	if(GtBit((short int)(tagBVal.value.val_di), 15) == 1)
		this->m_StatusA.Format(_T("bypassed"));
	else
		this->m_StatusA.Format(_T("active"));

	if(GtBit((short int)(tagAVal.value.val_di), 15) == 1)
		this->m_StatusB.Format(_T("bypassed"));
	else
		this->m_StatusB.Format(_T("active"));

	UpdateData(false);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

bool CSimulCtrl::SetControls(void){
	RETSTAT		retSt;
	int			simFlag;

	retSt = sysInSimulation(&simFlag);
	if(retSt != RT_NORMAL){
		MessageBox(_T("Error checking system simulation status."),  _T("Error"), MB_OK | MB_ICONERROR);
		return(FALSE);
	}

	//Controllo l'utente//
	retSt = GetLoggedUserLevel(theApp.m_pDlg->m_opStation, &this->m_loggedUser);
	if(retSt != RT_NORMAL){
		MessageBox(_T("Unknown logged user."),  _T("Error"), MB_OK);
		return false;
	}
	switch(this->m_loggedUser){
	case SYSMGR:
	case LEVEL1:
	case LEVEL2:
		if(simFlag){
			this->m_Reset.EnableWindow(true);
			this->m_BypassACtrl.EnableWindow(true);
			this->m_BypassBCtrl.EnableWindow(true);
			this->m_StatusACtrl.EnableWindow(true);
			this->m_StatusBCtrl.EnableWindow(true);
		}
		else{
			this->m_Reset.EnableWindow(false);
			this->m_BypassACtrl.EnableWindow(false);
			this->m_BypassBCtrl.EnableWindow(false);
			this->m_StatusACtrl.EnableWindow(false);
			this->m_StatusBCtrl.EnableWindow(false);
		}
		break;
	case LEVEL3:
	case LEVEL4:
		if(simFlag){
			this->m_Reset.EnableWindow(false);
			this->m_BypassACtrl.EnableWindow(true);
			this->m_BypassBCtrl.EnableWindow(true);
			this->m_StatusACtrl.EnableWindow(true);
			this->m_StatusBCtrl.EnableWindow(true);
		}
		else{
			this->m_Reset.EnableWindow(false);
			this->m_BypassACtrl.EnableWindow(false);
			this->m_BypassBCtrl.EnableWindow(false);
			this->m_StatusACtrl.EnableWindow(false);
			this->m_StatusBCtrl.EnableWindow(false);
		}
		break;
	case LEVEL5:
		if(simFlag){
			this->m_Reset.EnableWindow(false);
			this->m_BypassACtrl.EnableWindow(false);
			this->m_BypassBCtrl.EnableWindow(false);
			this->m_StatusACtrl.EnableWindow(false);
			this->m_StatusBCtrl.EnableWindow(false);
		}
		else{
			this->m_Reset.EnableWindow(false);
			this->m_BypassACtrl.EnableWindow(false);
			this->m_BypassBCtrl.EnableWindow(false);
			this->m_StatusACtrl.EnableWindow(false);
			this->m_StatusBCtrl.EnableWindow(false);
		}
		break;
	}
	return true;
}

bool CSimulCtrl::CheckUser(){
	RETSTAT		retSt;
	int			currentUser;
	
	currentUser = this->m_loggedUser;
	//Controllo l'utente//
	retSt = GetLoggedUserLevel(theApp.m_pDlg->m_opStation, &this->m_loggedUser);
	if(retSt != RT_NORMAL){
		MessageBox(_T("Unknown logged user."),  _T("Error"), MB_OK);
		return false;
	}
	if(m_loggedUser != currentUser){
		SetControls();
		return true;
	}
	return false;
}

void CSimulCtrl::OnReset() 
{
	RETSTAT		retSt;
	RETCOND		retCnd;
	TAGIDX		tagIdx;
	TAGVALEX	tagVal;
	int			simFlag;
	int			risp;

	if(CheckUser())
		return;

	retSt = sysInSimulation(&simFlag);
	if(retSt != RT_NORMAL){
		MessageBox(_T("Error checking system simulation status."),  _T("Error"), MB_OK | MB_ICONERROR);
		return;
	}

	if(!simFlag){
		MessageBox(_T("Cannot reset simulator while system is in control mode."),  _T("Error"), MB_OK | MB_ICONERROR);
		return;
	}
	
	risp = MessageBox(_T("Confirm Simulator reset?"), _T("Please confirm"), MB_YESNO | MB_ICONQUESTION);
	if(risp == IDYES){
		retCnd = GetTagIdx(TWN_SIM_RESET, &tagIdx);
		if(retCnd != R_NORMAL){
			MessageBox(_T("Error retrieving tag \"SIM_RESET\" identifier."),  _T("Error"), MB_OK | MB_ICONERROR);
			return;
		}
		retCnd = TagRead(&tagIdx, &tagVal);
		if(retCnd != R_NORMAL){
			MessageBox(_T("Error reading tag \"SIM_RESET\" value."),  _T("Error"), MB_OK | MB_ICONERROR);
			return;
		}
		StBit((unsigned short *)(&tagVal.value.val_di), 15);
		retCnd = TagWrite(&tagIdx, &tagVal, TAGWRITE_DIRECT);
		if(retCnd != R_NORMAL){
			MessageBox(_T("Error writing tag \"SIM_RESET\" value."),  _T("Error"), MB_OK | MB_ICONERROR);
			return;
		}
	}	
}

void CSimulCtrl::OnExit() 
{
	CDialog::OnCancel();
}


void CSimulCtrl::OnSetfocusSysStatus() 
{
	SetFocus();	
}

void CSimulCtrl::OnSetfocusAStatus() 
{
	SetFocus();	
}

void CSimulCtrl::OnSetfocusBStatus() 
{
	SetFocus();	
}

void CSimulCtrl::OnBypassA(){
	RETSTAT		retSt;
	RETCOND		retCnd;
	TAGIDX		tagIdx;
	TAGVALEX	tagVal;
	TCHAR		msg[128];
	int			risp;
	int			simFlag;
	int			ctrlFlag;

	retCnd = GetTagIdx(TWN_BYPASS_B, &tagIdx);
	if(retCnd != R_NORMAL){
		_stprintf_s(msg, _T("Error retrieving tag \"%s\" idx."), TWN_BYPASS_B);
		MessageBox(msg,  _T("Error"), MB_OK | MB_ICONERROR);
		return;
	}
	
	retCnd = TagRead(&tagIdx, &tagVal);
	if(retCnd != R_NORMAL){
		_stprintf_s(msg, _T("Error reading tag \"%s\" value."), TWN_BYPASS_B);
		MessageBox(msg,  _T("Error"), MB_OK | MB_ICONERROR);
		return;
	}

	if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
		risp = MessageBox(_T("Channel A is currently bypassed, acivate it?"), _T("Please confirm"), MB_YESNO | MB_ICONQUESTION);
		if(risp == IDYES){		
			//ClBit((unsigned short *)(&tagVal.value.val_di), 15);
			tagVal.value.val_di = 0;
			retCnd = TagWrite(&tagIdx, &tagVal, TAGWRITE_STD);
			if(retCnd != R_NORMAL){
				_stprintf_s(msg, _T("Error writing tag \"%s\" value."), TWN_BYPASS_B);
				MessageBox(msg,  _T("Error"), MB_OK | MB_ICONERROR);
				return;
			}
			retSt = SetGood(TWN_BYPASS_B, false);
			if(retSt != RT_NORMAL){
				_stprintf_s(msg, _T("Error setting tag \"%s\" quality."), TWN_BYPASS_B);
				MessageBox(msg,  _T("Error"), MB_OK | MB_ICONERROR);
				return;
			}
			this->m_StatusA.Format(_T("active"));
		}
		else
			return;
	}
	else{
		retSt = sysInSimulation(&simFlag);
		if(retSt != RT_NORMAL){
			MessageBox(_T("Error checking system simulation status."),  _T("Error"), MB_OK | MB_ICONERROR);
			return;
		}

		retSt = sysInControl(&ctrlFlag);
		if(retSt != RT_NORMAL){
			MessageBox(_T("Error checking system control status."),  _T("Error"), MB_OK | MB_ICONERROR);
			return;
		}
		if((simFlag == 0) && (ctrlFlag == 1)){
			MessageBox(_T("Cannot bypass channel A while system is in control mode."), _T("Warning"), MB_OK | MB_ICONERROR);
			return;
		}
		risp = MessageBox(_T("Channel A is currently activated, bypass it?"), _T("Please confirm"), MB_YESNO | MB_ICONQUESTION);
		if(risp == IDYES){
			//StBit((unsigned short *)(&tagVal.value.val_di), 15);
			tagVal.value.val_di = 1;
			retCnd = TagWrite(&tagIdx, &tagVal, TAGWRITE_STD);
			if(retCnd != R_NORMAL){
				_stprintf_s(msg, _T("Error writing tag \"%s\" value."), TWN_BYPASS_B);
				MessageBox(msg,  _T("Error"), MB_OK | MB_ICONERROR);
				return;
			}
			retSt = SetGood(TWN_BYPASS_B, false);
			if(retSt != RT_NORMAL){
				_stprintf_s(msg, _T("Error setting tag \"%s\" quality."), TWN_BYPASS_B);
				MessageBox(msg,  _T("Error"), MB_OK | MB_ICONERROR);
				return;
			}
			this->m_StatusA.Format(_T("bypassed"));
		}
		else
			return;
	}
	this->UpdateData(false);
}

void CSimulCtrl::OnBypassB(){
	RETSTAT		retSt;
	RETCOND		retCnd;
	TAGIDX		tagIdx;
	TAGVALEX	tagVal;
	TCHAR		msg[128];
	int			risp;
	int			simFlag;
	int			ctrlFlag;

	retCnd = GetTagIdx(TWN_BYPASS_A, &tagIdx);
	if(retCnd != R_NORMAL){
		_stprintf_s(msg, _T("Error retrieving tag \"%s\" idx."), TWN_BYPASS_A);
		MessageBox(msg,  _T("Error"), MB_OK | MB_ICONERROR);
		return;
	}
	
	retCnd = TagRead(&tagIdx, &tagVal);
	if(retCnd != R_NORMAL){
		_stprintf_s(msg, _T("Error reading tag \"%s\" value."), TWN_BYPASS_A);
		MessageBox(msg,  _T("Error"), MB_OK | MB_ICONERROR);
		return;
	}

	if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
		risp = MessageBox(_T("Channel B is currently bypassed, acivate it?"), _T("Please confirm"), MB_YESNO | MB_ICONQUESTION);
		if(risp == IDYES){		
			//ClBit((unsigned short *)(&tagVal.value.val_di), 15);
			tagVal.value.val_di = 0;
			retCnd = TagWrite(&tagIdx, &tagVal, TAGWRITE_STD);
			if(retCnd != R_NORMAL){
				_stprintf_s(msg, _T("Error writing tag \"%s\" value."), TWN_BYPASS_A);
				MessageBox(msg,  _T("Error"), MB_OK | MB_ICONERROR);
				return;
			}
			retSt = SetGood(TWN_BYPASS_A, false);
			if(retSt != RT_NORMAL){
				_stprintf_s(msg, _T("Error setting tag \"%s\" quality."), TWN_BYPASS_A);
				MessageBox(msg,  _T("Error"), MB_OK | MB_ICONERROR);
				return;
			}
			this->m_StatusB.Format(_T("active"));
		}
		else
			return;
	}
	else{
		retSt = sysInSimulation(&simFlag);
		if(retSt != RT_NORMAL){
			MessageBox(_T("Error checking system simulation status."),  _T("Error"), MB_OK | MB_ICONERROR);
			return;
		}

		retSt = sysInControl(&ctrlFlag);
		if(retSt != RT_NORMAL){
			MessageBox(_T("Error checking system control status."),  _T("Error"), MB_OK | MB_ICONERROR);
			return;
		}
		if((simFlag == 0) && (ctrlFlag == 1)){
			MessageBox(_T("Cannot bypass channel A while system is in control mode."), _T("Warning"), MB_OK | MB_ICONERROR);
			return;
		}
		risp = MessageBox(_T("Channel B is currently activated, bypass it?"), _T("Please confirm"), MB_YESNO | MB_ICONQUESTION);
		if(risp == IDYES){
			//StBit((unsigned short *)(&tagVal.value.val_di), 15);
			tagVal.value.val_di = 1;
			retCnd = TagWrite(&tagIdx, &tagVal, TAGWRITE_STD);
			if(retCnd != R_NORMAL){
				_stprintf_s(msg, _T("Error writing tag \"%s\" value."), TWN_BYPASS_A);
				MessageBox(msg,  _T("Error"), MB_OK | MB_ICONERROR);
				return;
			}
			retSt = SetGood(TWN_BYPASS_A, false);
			if(retSt != RT_NORMAL){
				_stprintf_s(msg, _T("Error setting tag \"%s\" quality."), TWN_BYPASS_A);
				MessageBox(msg,  _T("Error"), MB_OK | MB_ICONERROR);
				return;
			}
			this->m_StatusB.Format(_T("bypassed"));
		}
		else
			return;
	}
	this->UpdateData(false);
}
